<?php

/**
 * 数据同步
 * File Name：Tongbu.php
 * File Encoding：UTF-8
 * File New Time：2014-6-6 11:06:25
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class Tongbu {

    private $data = '';

    /**
     * 链接同步服务
     * @staticvar null $handier
     * @return \TaoAPI
     */
    static public function getInstance() {
        static $handier = NULL;
        if (empty($handier)) {
            $handier = new Tongbu();
        }
        return $handier;
    }

    /**
     * 设置数据
     * @param type $data
     * @return \Tongbu
     */
    public function data($data) {
        if ($this->isDimensional($data) !== false) {
            $data = array($data);
        }
        $this->data = $data;
        return $this;
    }

    //疯狂购
    public function fengkg($data = '', $url = 'http://94258.uz.taobao.com/view/front/sync.php') {
        if (empty($data) && !is_array($data)) {
            if (empty($this->data)) {
                return false;
            }
            $data = $this->data;
            $this->data = '';
        }
        if ($this->isDimensional($data) !== false) {
            $data = array($data);
        }
        $file = '';
        foreach ($data as $val) {
            foreach ($val as $k => $v) {
                $val[$k] = iconv("UTF-8", "GB2312//IGNORE", $v);
            }
            $file .= urlencode($val['product_id']) . "--wd--";
            $file .= urlencode($val['brand_id']) . "--wd--";
            $file .= urlencode($val['goods_name']) . "--wd--";
            $file .= urlencode($val['goods_brief']) . "--wd--";
            $file .= urlencode($val['item_type']) . "--wd--";
            $file .= urlencode($val['is_best']) . "--wd--";
            $file .= urlencode($val['is_new']) . "--wd--";
            $file .= urlencode($val['is_hot']) . "--wd--";
            $file .= urlencode($val['level']) . "--wd--";
            $file .= urlencode($val['detail_url']) . "--wd--";
            $file .= urlencode($val['product_img']) . "--wd--";
            $file .= urlencode($val['home_img']) . "--wd--";
            $file .= urlencode($val['module_img']) . "--wd--";
            $file .= urlencode($val['add_time']) . "--wd--";
            $file .= urlencode($val['status']) . "--wd--";
            $file .= urlencode($val['listorder']) . "--wd--";
            $file .= urlencode($val['cat_id']) . "--wd--";
            $file .= urlencode($val['moduleid']) . "--wd--";
            $file .= urlencode($val['p_price']) . "--wd--";
            $file .= urlencode($val['commission_rate']) . "--wd--";
            $file .= urlencode($val['promo_price']) . "--wd--";
            $file .= urlencode($val['start_time']) . "--wd--";
            $file .= urlencode($val['end_time']) . "--wd--";
            $file .= urlencode($val['nick']) . "--wd--";
            $file .= urlencode($val['nick_add']) . "--wd--";
            $file .= urlencode($val['state']) . "--wd--";
            $file .= urlencode($val['city']) . "--wd--";
            $file .= urlencode($val['is_stock']) . "--wd--";
            $file .= urlencode($val['stock']) . "--wd--";
            $file .= urlencode($val['freeshipping']) . "--wd--";
            $file .= urlencode($val['is_vip']) . "--wd--";
            $file .= urlencode($val['sales']) . "--wd--";
            $file .= "--le--";
        }
        $post_data = array(
            "password" => "121khksdhaiu001823123kjahsdkjaoiyeiqddjsjhga",
            "result" => $file,
        );
        foreach ($post_data as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        //开发模式不同步
        if (defined('YII_DEBUG') && YII_DEBUG) {
            return true;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($post_data));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        //不显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    //淘牛品，返回的是同步失败数，0是全部同步成功
    public function taonp($data = '', $url = 'http://tealife.uz.taobao.com/view/front/sync.php') {
        if (empty($data) && !is_array($data)) {
            if (empty($this->data)) {
                return false;
            }
            $data = $this->data;
            $this->data = '';
        }
        if ($this->isDimensional($data) !== false) {
            $data = array($data);
        }
        $cat_ids = array(
            '1' => '43',
            '2' => '44',
            '3' => '45',
            '4' => '46',
            '5' => '47',
            '6' => '48',
            '7' => '49',
            '8' => '52',
            '9' => '50',
            '10' => '54',
            '11' => '55',
            '12' => '67',
            '13' => '68',
        );
        $module_ids = array(
            '12' => '50',
            '13' => '51',
            '14' => '52',
            '15' => '54',
            '16' => '56',
            '17' => '57',
            '18' => '53',
            '19' => '55',
            '20' => '58',
            '21' => '59',
            '22' => '60',
            '23' => '61',
        );
        $levels = array(
            '7' => '100',
            '5' => '101',
            '103' => '1002',
        );
        $file = '';
        foreach ($data as $val) {
            foreach ($val as $k => $v) {
                $val[$k] = iconv("UTF-8", "GB2312//IGNORE", $v);
            }
            $file .= urlencode($val['product_id']) . "--wd--";
            $file .= urlencode($val['brand_id']) . "--wd--";
            $file .= urlencode($val['goods_name']) . "--wd--";
            $file .= urlencode($val['goods_brief']) . "--wd--";
            $file .= urlencode($val['item_type']) . "--wd--";
            $file .= urlencode($val['is_best']) . "--wd--";
            $file .= urlencode($val['is_new']) . "--wd--";
            $file .= urlencode($val['is_hot']) . "--wd--";
            $level = $levels[$val['level']] ? $levels[$val['level']] : $val['level'];
            $file .= urlencode($level) . "--wd--";
            $file .= urlencode($val['detail_url']) . "--wd--";
            $file .= urlencode($val['product_img']) . "--wd--";
            $file .= urlencode($val['home_img']) . "--wd--";
            $file .= urlencode($val['module_img']) . "--wd--";
            $file .= urlencode($val['add_time']) . "--wd--";
            $file .= urlencode($val['status']) . "--wd--";
            $file .= urlencode($val['listorder']) . "--wd--";
            $cat_id = $cat_ids[$val['cat_id']] ? $cat_ids[$val['cat_id']] : $val['cat_id'];
            $file .= urlencode($cat_id) . "--wd--";
            $moduleid = $module_ids[$val['moduleid']] ? $module_ids[$val['moduleid']] : $val['moduleid'];
            $file .= urlencode($moduleid) . "--wd--";
            $file .= urlencode($val['p_price']) . "--wd--";
            $file .= urlencode($val['commission_rate']) . "--wd--";
            $file .= urlencode($val['promo_price']) . "--wd--";
            $file .= urlencode($val['start_time']) . "--wd--";
            $file .= urlencode($val['end_time']) . "--wd--";
            $file .= urlencode($val['nick']) . "--wd--";
            $file .= urlencode($val['nick_add']) . "--wd--";
            $file .= urlencode($val['state']) . "--wd--";
            $file .= urlencode($val['city']) . "--wd--";
            $file .= urlencode($val['is_stock']) . "--wd--";
            $file .= urlencode($val['stock']) . "--wd--";
            $file .= urlencode($val['freeshipping']) . "--wd--";
            $file .= urlencode($val['is_vip']) . "--wd--";
            $file .= urlencode($val['sales']) . "--wd--";
            $file .= "--le--";
        }
        $post_data = array(
            "password" => "121khksdhaiu001823123kjahsdkjaoiyeiqddjsjhga",
            "result" => $file,
        );
        foreach ($post_data as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        //开发模式不同步
        if (defined('YII_DEBUG') && YII_DEBUG) {
            return true;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($post_data));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        //不显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
        if (!empty($result)) {
            preg_match('/@@@@@(.*)@@@@@/', $result, $match);
            return (int) $match[1];
        }
        return 0;
    }

    protected function isDimensional($data) {
        if (is_array($data)) {
            return false;
        }
        return is_array(current($data)) ? true : false;
    }

}
